import { AppParams } from "@/utils/appParams";
import { Device } from "@/api/DeviceApi";

const state = {

  /**
   * 弃用driver，请使用 /src/store/flow/flowStore.ts 下的 Device 替代
   * @deprecated
   */
  driver: [],
  databases: [{ Name: '内置', Id: 1 }],
  plcAddress: [],
  GlobalParams: []
}

/**
 * 更改状态管理器的变量
 * @type {{SET_DRIVER: mutations.SET_DRIVER, SET_PLC_ADDRESS: mutations.SET_PLC_ADDRESS}}
 */
const mutations = {
  SET_DRIVER: (state, data) => {
    state.driver = data
  },
  SET_PLC_ADDRESS: (state, data) => {
    state.plcAddress = data
  },
  SET_GLOBAL_PARAMS: (state, data) => {
    state.GlobalParams = data
  }
}

/**
 * 触发 mutations 去更改状态管理器的变量
 * @type {{updatePlcAddress({commit: *, getters: *}, *): void, updateDriver({commit: *, getters: *}, *): void}}
 */
const actions = {

  /**
   * 获取设备数据
   *
   * 弃用，请使用 /src/store/flow/flowStore.ts 下的 Device 替代
   * @deprecated
   * @param commit
   * @param getters
   * @param data
   */
  updateDriver ({ commit, getters }, data) {
    Device.Gets().then(res => {
      commit('SET_DRIVER', res.Data)
    })
  },

  /**
   * 获取PLC地址
   * @param commit
   * @param getters
   * @param data
   */
  // updatePlcAddress ({ commit, getters }, data) {
  //   Device.GetPlcAddress(AppParams.DRIVER_TYPE_PLC).then(res=>{
  //     commit('SET_PLC_ADDRESS', res)
  //   })
  // }
}

const getters = {
  getPlcAddress: state => state.driver
}

export default {
  namespaced: true,
  state, mutations, actions, getters
}
